/**
 * 
 */
package com.dalonedrow.engine.ui.impl.border;

import com.dalonedrow.engine.sprite.base.SimpleVector3;
import com.dalonedrow.engine.sprite.base.SpriteImageObject;
import com.dalonedrow.engine.sprite.base.SpriteImageObjectFactory;
import com.dalonedrow.engine.sprite.impl.BasicQuadSpriteObject;
import com.dalonedrow.engine.sprite.impl.BasicSpriteObject;
import com.dalonedrow.engine.sprite.impl.GroupOfImages;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;
import com.dalonedrow.engine.systems.base.ProjectConstants;
import com.dalonedrow.engine.ui.base.GuiComponent;
import com.dalonedrow.engine.ui.base.border.GuiBorder;
import com.dalonedrow.engine.ui.base.panel.TabbedPanel;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;

/**
 * @author Donald
 */
@SuppressWarnings("unused")
public final class RightTabbedGuiBorder extends GuiBorder {
	/** the border group. */
	private GroupOfImages			borders;
	/** the middle sprite. */
	private BasicQuadSpriteObject	middle;
	/** the x-offset for the middle graphic. */
	private int						middleInsetX;
	/** the y-offset for the middle graphic. */
	private int						middleInsetY;
	/** the tab button associated with the content. */
	private GuiComponent			tab;
	/** the tab order: first, middle, or last. */
	private int						tabAlignment;
	/**
	 * flag indicating whether supplied offsets are used for middle background.
	 */
	private boolean					useOffsets;
	/**
	 * Creates a new instance of {@link RightTabbedGuiBorder}.
	 * @param insets the border insets
	 * @param corners the corner images
	 * @param background the background color
	 * @param tabPosition the tab position
	 * @param newTab the tab component
	 */
	public RightTabbedGuiBorder(final int[] insets, final String[] corners,
			final float[] background, final int tabPosition,
			final GuiComponent newTab) {
		this(insets, corners, background, 0, 0, tabPosition, newTab);
	}
	/**
	 * Creates a new instance of {@link RightTabbedGuiBorder}.
	 * @param insets the border insets
	 * @param corners the corner images
	 * @param background the background color
	 * @param xOffset the x-offset for the middle background
	 * @param yOffset the y-offset for the middle background
	 * @param tabPosition the tab position
	 * @param newTab the tab component
	 */
	public RightTabbedGuiBorder(final int[] insets, final String[] corners,
			final float[] background, final int xOffset, final int yOffset,
			final int tabPosition, final GuiComponent newTab) {
		final int three = 3, four = 4, eight = 8;
		if (insets == null
				|| insets != null
						&& insets.length != four) {
			super.setInsets(
					eight,
					eight,
					eight,
					eight
					);
		} else {
			super.setInsets(insets);
		}
		if (corners == null
				|| corners != null
						&& corners.length < eight) {
			JOGLErrorHandler.getInstance().fatalError(new Exception(
					"At least 8 images are needed."));
		}
		if (background != null
				&& background.length < four) {
			JOGLErrorHandler.getInstance().fatalError(new Exception(
					"3 or 4-color background is needed."));
		}
		middleInsetX = xOffset;
		middleInsetY = yOffset;
		tabAlignment = tabPosition;
		tab = newTab;
		switch (tabAlignment) {
		case TAB_FIRST:
			createForFirstTab(corners, background);
			break;
		case TAB_MIDDLE:
			createForMiddleTab(corners, background);
			break;
		default:
			createForLastTab(corners, background);
			break;
		}
	}
	/**
	 * Creates the border images used for displaying content next to the first
	 * tab.
	 * @param corners the list of corner images
	 * @param background the background color. null if no background is needed
	 */
	private void createForFirstTab(final String[] corners,
			final float[] background) {
		try {
			final int three = 3, four = 4, eight = 8;
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_00BOTTOM_LEFT]);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_01BOTTOM]);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_02BOTTOM_RIGHT]);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_05TOP_LEFT]);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_06TOP]);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_06TOP]);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_03LEFT]);
			SpriteImageObject imgRB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_13_REV_TOP_LEFT]);
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_04RIGHT]);
			borders = new GroupOfImages();
			// sprite images don't get set until the preferred size is set
			if (middleInsetX != -1
					&& middleInsetY != -1) {
				useOffsets = true;
			}
			if (middleInsetX == -1) {
				middleInsetX = imgL.getWidth();
			}
			if (middleInsetY == -1) {
				middleInsetY = imgB.getHeight();
			}

			// bottom left
			BasicSpriteObject sb = new BasicSpriteObject();
			sb.setImageRefId(imgBL.getRefId());
			sb.setWidth(imgBL.getWidth());
			sb.setHeight(imgBL.getHeight());
			borders.addImage(sb);
			// bottom middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgB.getRefId());
			sb.setHeight(imgB.getHeight());
			borders.addImage(sb);
			// bottom right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgBR.getRefId());
			sb.setWidth(imgBR.getWidth());
			sb.setHeight(imgBR.getHeight());
			borders.addImage(sb);
			// left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgL.getRefId());
			sb.setWidth(imgL.getWidth());
			borders.addImage(sb);
			// right-bottom
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgRB.getRefId());
			sb.setWidth(imgRB.getWidth());
			sb.setHeight(imgRB.getHeight());
			borders.addImage(sb);
			// right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgR.getRefId());
			sb.setWidth(imgR.getWidth());
			borders.addImage(sb);
			// top left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTL.getRefId());
			sb.setWidth(imgTL.getWidth());
			sb.setHeight(imgT.getHeight());
			borders.addImage(sb);
			// top middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgT.getRefId());
			sb.setHeight(imgT.getHeight());
			borders.addImage(sb);
			// top right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTR.getRefId());
			sb.setWidth(imgTR.getWidth());
			sb.setHeight(imgTR.getHeight());
			borders.addImage(sb);
			// middle
			if (background != null) {
				middle = new BasicQuadSpriteObject();
				middle.setRed(background[0]);
				middle.setGreen(background[1]);
				middle.setBlue(background[2]);
				if (background.length > four) {
					middle.setAlpha(background[four]);
				} else {
					middle.setAlpha(1f);
				}
			}

			// clear local variables
			sb = null;
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgRB = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Creates the border images used for displaying content next to the last
	 * tab.
	 * @param corners the list of corner images
	 * @param background the background color. null if no background is needed
	 */
	private void createForLastTab(final String[] corners,
			final float[] background) {
		try {
			final int three = 3, four = 4, eight = 8;
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_00BOTTOM_LEFT]);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_01BOTTOM]);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_01BOTTOM]);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_06TOP]);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_07TOP_RIGHT]);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_06TOP]);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_03LEFT]);
			SpriteImageObject imgRT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_08_REV_BOTTOM_LEFT]);
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_04RIGHT]);
			borders = new GroupOfImages();
			// sprite images don't get set until the preferred size is set
			if (middleInsetX != -1
					&& middleInsetY != -1) {
				useOffsets = true;
			}
			if (middleInsetX == -1) {
				middleInsetX = imgL.getWidth();
			}
			if (middleInsetY == -1) {
				middleInsetY = imgB.getHeight();
			}

			// bottom left
			BasicSpriteObject sb = new BasicSpriteObject();
			sb.setImageRefId(imgBL.getRefId());
			sb.setWidth(imgBL.getWidth());
			sb.setHeight(imgBL.getHeight());
			borders.addImage(sb);
			// bottom middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgB.getRefId());
			sb.setHeight(imgB.getHeight());
			borders.addImage(sb);
			// bottom right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgBR.getRefId());
			// USE RIGHT WIDTH
			sb.setWidth(imgR.getWidth());
			sb.setHeight(imgBR.getHeight());
			borders.addImage(sb);
			// left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgL.getRefId());
			sb.setWidth(imgL.getWidth());
			borders.addImage(sb);
			// right-top
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgRT.getRefId());
			sb.setWidth(imgRT.getWidth());
			sb.setHeight(imgRT.getHeight());
			borders.addImage(sb);
			// right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgR.getRefId());
			sb.setWidth(imgR.getWidth());
			borders.addImage(sb);
			// top left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTL.getRefId());
			sb.setWidth(imgTL.getWidth());
			sb.setHeight(imgTL.getHeight());
			borders.addImage(sb);
			// top middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgT.getRefId());
			sb.setHeight(imgT.getHeight());
			borders.addImage(sb);
			// top right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTR.getRefId());
			sb.setWidth(imgTR.getWidth());
			sb.setHeight(imgTR.getHeight());
			borders.addImage(sb);
			// middle
			if (background != null) {
				middle = new BasicQuadSpriteObject();
				middle.setRed(background[0]);
				middle.setGreen(background[1]);
				middle.setBlue(background[2]);
				if (background.length > four) {
					middle.setAlpha(background[four]);
				} else {
					middle.setAlpha(1f);
				}
			}

			// clear local variables
			sb = null;
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgRT = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Creates the border images used for displaying content next to a middle
	 * tab.
	 * @param corners the list of corner images
	 * @param background the background color. null if no background is needed
	 */
	private void createForMiddleTab(final String[] corners,
			final float[] background) {
		try {
			final int three = 3, four = 4, eight = 8;
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_00BOTTOM_LEFT]);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_01BOTTOM]);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_02BOTTOM_RIGHT]);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_05TOP_LEFT]);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_07TOP_RIGHT]);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_06TOP]);
			SpriteImageObject imgR1 =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_04RIGHT]);
			SpriteImageObject imgRT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_08_REV_BOTTOM_LEFT]);
			SpriteImageObject imgRB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_13_REV_TOP_LEFT]);
			SpriteImageObject imgR2 =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_04RIGHT]);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							corners[GlobalConstants.CORNER_03LEFT]);
			borders = new GroupOfImages();
			// sprite images don't get set until the preferred size is set
			if (middleInsetX != -1
					&& middleInsetY != -1) {
				useOffsets = true;
			}
			if (middleInsetX == -1) {
				middleInsetX = imgL.getWidth();
			}
			if (middleInsetY == -1) {
				middleInsetY = imgB.getHeight();
			}

			// bottom left
			BasicSpriteObject sb = new BasicSpriteObject();
			sb.setImageRefId(imgBL.getRefId());
			sb.setWidth(imgBL.getWidth());
			sb.setHeight(imgBL.getHeight());
			borders.addImage(sb);
			// bottom middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgB.getRefId());
			sb.setHeight(imgB.getHeight());
			borders.addImage(sb);
			// bottom right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgBR.getRefId());
			sb.setWidth(imgBR.getWidth());
			sb.setHeight(imgBR.getHeight());
			borders.addImage(sb);
			// right-one
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgR1.getRefId());
			sb.setWidth(imgR1.getWidth());
			borders.addImage(sb);
			// right-bottom
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgRB.getRefId());
			sb.setWidth(imgRB.getWidth());
			sb.setHeight(imgRB.getHeight());
			borders.addImage(sb);
			// right-top
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgRT.getRefId());
			sb.setWidth(imgRT.getWidth());
			sb.setHeight(imgRT.getHeight());
			borders.addImage(sb);
			// right-two
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgR2.getRefId());
			sb.setWidth(imgR2.getWidth());
			borders.addImage(sb);
			// left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgL.getRefId());
			sb.setWidth(imgL.getWidth());
			borders.addImage(sb);
			// top left
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTL.getRefId());
			sb.setWidth(imgTL.getWidth());
			sb.setHeight(imgTL.getHeight());
			borders.addImage(sb);
			// top middle
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgT.getRefId());
			sb.setHeight(imgT.getHeight());
			borders.addImage(sb);
			// top right
			sb = new BasicSpriteObject();
			sb.setImageRefId(imgTR.getRefId());
			sb.setWidth(imgTR.getWidth());
			sb.setHeight(imgTR.getHeight());
			borders.addImage(sb);
			// middle
			if (background != null) {
				middle = new BasicQuadSpriteObject();
				middle.setRed(background[0]);
				middle.setGreen(background[1]);
				middle.setBlue(background[2]);
				if (background.length > four) {
					middle.setAlpha(background[four]);
				} else {
					middle.setAlpha(1f);
				}
			}

			// clear local variables
			sb = null;
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgR1 = null;
			imgRB = null;
			imgRT = null;
			imgR2 = null;
			imgL = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(borders);
		if (middle != null) {
			Render.getInstance().registerEntity(middle);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y,
			final double z) {
		super.setPosition(x, y, z);
		borders.setPosition(x, y, z);
		borders.clearFloatStore();
		if (middle != null) {
			middle.setPosition(x + middleInsetX, y + middleInsetY, z);
			middle.clearFloatStore();
		}
	}
	/**
	 * {@inheritDoc}
	 */
	public void setPreferredSize(final int width, final int height,
			final TabbedPanel parent, final int tabIndex) {
		super.setPreferredSize(width, height);
		switch (tabAlignment) {
		case TAB_FIRST:
			setSizeForFirst(width, height);
			break;
		case TAB_MIDDLE:
			setSizeForMiddle(width, height, parent, tabIndex);
			break;
		default:
			setSizeForLast(width, height);
			break;
		}
	}
	/**
	 * Sets the size of the border for the first tab.
	 * @param width the border width
	 * @param height the border height
	 */
	private void setSizeForFirst(final int width, final int height) {
		try {
			int i = 0;
			// get images
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgRB =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			// SET POSITIONS
			i = 0;
			// set bottom-left position
			borders.getImage(i++).setPosition(new SimpleVector3(0, 0, 0));
			// set bottom position and width
			borders.getImage(i).setWidth(
					width - imgBL.getWidth() - imgBR.getWidth());
			borders.getImage(i++).setPosition(
					new SimpleVector3(imgBL.getWidth(), 0, 0));
			// set bottom-right position
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgBR.getWidth(), 0, 0));
			// set left position and height
			borders.getImage(i).setHeight(
					height - imgBL.getHeight() - imgTL.getHeight());
			borders.getImage(i++).setPosition(
					new SimpleVector3(0, imgBL.getHeight(), 0));
			// set right-bottom position
			int rh = height;
			rh -= imgBR.getHeight();
			rh -= tab.getPreferredSize().height;
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgR.getWidth(),
							imgBR.getHeight() + rh, 0));
			// set right position and height
			borders.getImage(i).setHeight(rh);
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgR.getWidth(), imgBR.getHeight(), 0));
			// set top-left position
			borders.getImage(i++).setPosition(
					new SimpleVector3(0, height - imgTL.getHeight(), 0));
			// set top position and width
			borders.getImage(i).setWidth(
					width - imgTL.getWidth() - imgTR.getWidth());
			borders.getImage(i++)
					.setPosition(
							new SimpleVector3(imgTL.getWidth(), height
									- imgT.getHeight(), 0));
			// set top-right position
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgTR.getWidth(),
							height - imgTR.getHeight(), 0));
			// set middle position and width
			if (middle != null) {
				if (useOffsets) {
					middle.setWidth(width - middleInsetX * 2);
					middle.setHeight(height - middleInsetY * 2);
				} else {
					middle.setWidth(width - imgL.getWidth() - imgR.getWidth());
					middle.setHeight(
							height - imgB.getHeight() - imgT.getHeight());
				}
			}
			if (super.getPosition() != null) {
				borders.setPosition(
						super.getPosition().getX(),
						super.getPosition().getY(),
						super.getPosition().getZ()
						);
				borders.clearFloatStore();
				if (middle != null) {
					middle.setPosition(
							super.getPosition().getX() + middleInsetX,
							super.getPosition().getY() + middleInsetY,
							super.getPosition().getZ()
							);
					middle.clearFloatStore();
				}
			}
			// clear local variables
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgRB = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Sets the size of the border for the last tab.
	 * @param width the border width
	 * @param height the border height
	 */
	private void setSizeForLast(final int width, final int height) {
		try {
			int i = 0;
			// get images
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgLT =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			// SET POSITIONS
			i = 0;
			// set bottom-left position
			borders.getImage(i++).setPosition(new SimpleVector3(0, 0, 0));
			// set bottom position and width
			borders.getImage(i++).setWidth(
					width - imgBL.getWidth() - imgBR.getWidth());
			borders.getImage(i).setPosition(
					new SimpleVector3(imgBL.getWidth(), 0, 0));
			// set bottom-right position
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgBR.getWidth(), 0, 0));
			// set left position and height
			int lh = height;
			lh -= imgTL.getHeight();
			lh -= tab.getPreferredSize().height;
			borders.getImage(i++).setHeight(lh);
			borders.getImage(i).setPosition(
					new SimpleVector3(0, tab.getPreferredSize().height, 0));
			// set left-top position
			borders.getImage(i).setPosition(new SimpleVector3(0,
					tab.getPreferredSize().height - imgLT.getHeight(), 0));
			// set right position and height
			borders.getImage(i++).setHeight(
					height - imgBR.getHeight() - imgTR.getHeight());
			borders.getImage(i).setPosition(
					new SimpleVector3(width - imgR.getWidth(), imgBR.getHeight(), 0));
			// set top-left position
			borders.getImage(i++).setPosition(
					new SimpleVector3(0, height - imgTL.getHeight(), 0));
			// set top position and width
			borders.getImage(i++).setWidth(
					width - imgTL.getWidth() - imgTR.getWidth());
			borders.getImage(i)
					.setPosition(
							new SimpleVector3(imgTL.getWidth(), height
									- imgT.getHeight(), 0));
			// set top-right position
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgTR.getWidth(),
							height - imgTR.getHeight(), 0));
			// set middle position and width
			if (middle != null) {
				if (useOffsets) {
					middle.setWidth(width - middleInsetX * 2);
					middle.setHeight(height - middleInsetY * 2);
				} else {
					middle.setWidth(width - imgL.getWidth() - imgR.getWidth());
					middle.setHeight(
							height - imgB.getHeight() - imgT.getHeight());
				}
			}
			if (super.getPosition() != null) {
				borders.setPosition(
						super.getPosition().getX(),
						super.getPosition().getY(),
						super.getPosition().getZ()
						);
				borders.clearFloatStore();
				if (middle != null) {
					middle.setPosition(
							super.getPosition().getX() + middleInsetX,
							super.getPosition().getY() + middleInsetY,
							super.getPosition().getZ()
							);
					middle.clearFloatStore();
				}
			}
			// clear local variables
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgLT = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Sets the border size for a middle tab.
	 * @param width the border width
	 * @param height the border height
	 * @param parent the parent panel
	 * @param tabIndex the index of the content being displayed
	 */
	private void setSizeForMiddle(final int width, final int height,
			final TabbedPanel parent, final int tabIndex) {
		try {
			// find tab offset
			int myTabBottom = 0, myTabHeight = 0;
			for (int i = 0; i <= tabIndex; i++) {
				GuiComponent nextTab = parent.getTab(i);
				myTabBottom += nextTab.getPreferredSize().height;
				if (i == tabIndex) {
					myTabHeight = nextTab.getPreferredSize().height;
				}
			}
			int i = 0;
			// get images
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgR1 =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgRB =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgRT =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgR2 =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
							);
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageById(
							borders.getImage(i++).getImageRefId()
								);
			// SET POSITIONS
			i = 0;
			// set bottom-left position
			borders.getImage(i++).setPosition(new SimpleVector3(0, 0, 0));
			// set bottom position and width
			borders.getImage(i).setWidth(
					width - imgBL.getWidth() - imgBR.getWidth());
			borders.getImage(i++).setPosition(
					new SimpleVector3(imgBL.getWidth(), 0, 0));
			// set bottom-right position
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgBR.getWidth(), 0, 0));
			// set right-one position and height
			int rh = height - imgBR.getHeight() - myTabBottom;
			borders.getImage(i).setHeight(rh);
			borders.getImage(i++).setPosition(new SimpleVector3(
					width - imgR1.getWidth(), imgBR.getHeight(), 0));
			// set right-bottom position
			borders.getImage(i++).setPosition(new SimpleVector3(
					width - imgRB.getWidth(), height - myTabBottom, 0));
			// set right-top position
			borders.getImage(i++).setPosition(new SimpleVector3(
					width - imgRT.getWidth(), height - myTabBottom
							+ myTabHeight - imgRT.getHeight(), 0));
			// set right-two position and height
			int rh2 = height;
			rh2 -= imgBR.getHeight();
			rh2 -= rh;
			rh2 -= myTabHeight;
			rh2 -= imgTR.getHeight();
			borders.getImage(i).setHeight(rh2);
			borders.getImage(i++).setPosition(
					new SimpleVector3(width - imgR2.getWidth(),
							imgBR.getHeight() + rh + myTabHeight, 0));
			// set left position and height
			borders.getImage(i).setHeight(
					height - imgBL.getHeight() - imgTL.getHeight());
			borders.getImage(i++).setPosition(new SimpleVector3(
					0, imgBL.getHeight(), 0));
			// set top-left position
			borders.getImage(i++).setPosition(
					new SimpleVector3(0, height - imgTL.getHeight(), 0));
			// set top position and width
			borders.getImage(i).setWidth(
					width - imgTL.getWidth() - imgTR.getWidth());
			borders.getImage(i++).setPosition(new SimpleVector3(
					imgTL.getWidth(), height - imgT.getHeight(), 0));
			// set top-right position
			borders.getImage(i++).setPosition(new SimpleVector3(
					width - imgTR.getWidth(), height - imgTR.getHeight(), 0));
			// set middle position and width
			if (middle != null) {
				if (useOffsets) {
					middle.setWidth(width - middleInsetX * 2);
					middle.setHeight(height - middleInsetY * 2);
				} else {
					middle.setWidth(width - imgR1.getWidth() - imgL.getWidth());
					middle.setHeight(
							height - imgB.getHeight() - imgT.getHeight());
				}
			}
			if (super.getPosition() != null) {
				borders.setPosition(
						super.getPosition().getX(),
						super.getPosition().getY(),
						super.getPosition().getZ()
						);
				borders.clearFloatStore();
				if (middle != null) {
					middle.setPosition(
							super.getPosition().getX() + middleInsetX,
							super.getPosition().getY() + middleInsetY,
							super.getPosition().getZ()
							);
					middle.clearFloatStore();
				}
			}
			// clear local variables
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgR1 = null;
			imgRB = null;
			imgRT = null;
			imgR2 = null;
			imgL = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
